package cn.fxl.order.service.impl;

import cn.fxl.basic.pageList.GmResult;
import cn.fxl.basic.utils.LoginContext;
import cn.fxl.order.domain.OrderPetAcquisition;
import cn.fxl.order.dto.OrderDto;
import cn.fxl.order.mapper.OrderPetAcquisitionMapper;
import cn.fxl.order.query.OrderPetAcquisitionQuery;
import cn.fxl.order.service.IOrderPetAcquisitionService;
import cn.fxl.basic.service.impl.BasicServiceImpl;
import cn.fxl.org.domain.Employee;
import cn.fxl.org.domain.Shop;
import cn.fxl.org.mapper.EmployeeMapper;
import cn.fxl.org.mapper.ShopMapper;
import cn.fxl.pet.domain.Pet;
import cn.fxl.pet.domain.SearchMasterMsg;
import cn.fxl.pet.mapper.PetMapper;
import cn.fxl.pet.mapper.SearchMasterMsgMapper;
import cn.fxl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BasicServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public GmResult<OrderPetAcquisition> PagequeryByRole(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.getByLogininfoId(logininfo.getId());
        Shop shop = shopMapper.getOne(employee.getShop_id());
        if(employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
            if(shop.getAdmin_id().equals(employee.getId())){
                query.setEmpId(employee.getId());
            }
        }
       return super.Pagequery(query);
    }

    /**
     * 订单确认
     * @param orderDto
     */
    @Override
    public void confirm(OrderDto orderDto) {
        //修改订单
        Long orderId = orderDto.getId();
        BigDecimal money = orderDto.getMoney();
        Integer payType = orderDto.getPayType();
        OrderPetAcquisition order = orderPetAcquisitionMapper.getOne(orderId);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(payType);
        orderPetAcquisitionMapper.update(order);
        //修改寻主消息
        SearchMasterMsg msg = searchMasterMsgMapper.getOne(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
        //修改宠物信息
        Pet pet = petMapper.getOne(order.getPetId());
        pet.setSaleprice(money);
        petMapper.update(pet);
    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    public void remove(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.getOne(id);
        Pet pet = petMapper.getOne(order.getPetId());
        SearchMasterMsg msg = searchMasterMsgMapper.getOne(order.getSearchMasterMsgId());
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);
        petMapper.remove(pet.getId());
        super.remove(id);
    }
}
